const axios = require('./axios');
const { ownerAndRepo } = require('./tool');

module.exports = {
  /**
   * 获取仓库README
   * @link https://gitee.com/api/v5/swagger#/getV5ReposOwnerRepoReadme
   **/
  readme: (option = {}) => {
    return axios({
      method: 'get',
      url: `repos/${ownerAndRepo(option)}/readme`,
      params: option
    })
  },
  /**
   * 新建文件
   * @link https://gitee.com/api/v5/swagger#/postV5ReposOwnerRepoContentsPath
   **/
  newFile: (option = {}) => {
    option = Object.assign({
      method: 'post',
      url: `repos/${ownerAndRepo(option)}/contents/${option.path}`,
      data: option,
    }, option)
    return axios(option)
  },
  /**
   * 更新文件
   * @link https://gitee.com/api/v5/swagger#/putV5ReposOwnerRepoContentsPath
   **/
  updateFile: (option = {}) => {
    return axios({
      method: 'put',
      url: `repos/${ownerAndRepo(option)}/contents/${option.path}`,
      data: option,
    })
  },
  /**
   * 删除文件
   * @link https://gitee.com/api/v5/swagger#/deleteV5ReposOwnerRepoContentsPath
   **/
  deleteFile: (option = {}) => {
    return axios({
      method: 'delete',
      url: `repos/${ownerAndRepo(option)}/contents/${option.path}`,
      data: option,
    })
  },
  /**
   * 获取仓库具体路径下的内容
   * @link https://gitee.com/api/v5/swagger#/getV5ReposOwnerRepoContents(Path)
   **/
  contents: (option = {}) => {
    return axios.get(`repos/${ownerAndRepo(option)}/contents/${option.path || ''}`, {
      params: option
    })
  },
  /**
   * 获取 raw 文件（100MB 以内）
   * @link https://gitee.com/api/v5/swagger#/getV5ReposOwnerRepoRawPath
   **/
  raw: (option = {}) => {
    return axios.get(`repos/${ownerAndRepo(option)}/raw/${option.path || ''}`, {
      params: option
    })
  },
};